package com.yungu.swift.system.finance.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.finance.dao.FinanceRefundMapper;
import com.yungu.swift.system.finance.model.dto.FinanceRefundDto;
import com.yungu.swift.system.finance.model.param.AdminRefundPageParam;
import com.yungu.swift.system.finance.model.vo.FinanceRefundVo;
import com.yungu.swift.system.finance.service.FinanceRefundService;
import com.yungu.swift.system.finance.model.enums.FinanceEnum;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FinanceRefundServiceImpl extends BaseServiceImpl<FinanceRefundDto> implements FinanceRefundService {

    @Autowired
    private FinanceRefundMapper financeRefundMapper;

    @Override
    protected IMapper<FinanceRefundDto> getMapper() {
        return financeRefundMapper;
    }

    @Override
    public ResponseData<PageVo<FinanceRefundVo>> queryPageVo(AdminRefundPageParam refundPageParam) {
        PageBounds pageBounds = new PageBounds(refundPageParam.getPageNum(), refundPageParam.getPageSize());
        if (StringUtils.isNotEmpty(refundPageParam.getSortString())) {
            pageBounds.setOrders(Order.formString(refundPageParam.getSortString()));
        }
        PageList<FinanceRefundVo> pageList = financeRefundMapper.queryPageVo(refundPageParam, pageBounds);
        fillDate(pageList);
        PageVo<FinanceRefundVo> pageVo = new PageVo(refundPageParam.getPageNum(), refundPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<List<FinanceRefundVo>> export(AdminRefundPageParam refundPageParam) {
        List<FinanceRefundVo> financeRefundVos = financeRefundMapper.queryPageVo(refundPageParam);
        fillDate(financeRefundVos);
        return ResponseData.buildSuccessResponse(financeRefundVos);
    }


    /**
     * 补充数据含义
     *
     * @param list
     */
    private void fillDate(List<FinanceRefundVo> list) {
        list.stream().forEach(financeRefundVo -> financeRefundVo.setCourseDescription(FinanceEnum.getMsg(financeRefundVo
                .getCourse()))
        );
    }

    @Override
    public ResponseData<BigDecimal> findRefundAmount(String orderUuid) {
        return ResponseData.buildSuccessResponse(financeRefundMapper.findRefundAmount(orderUuid));
    }
}